/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.io.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ResourceBundle;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import offset.nodes.Constants;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.model.ServerResponse;
import offset.nodes.client.dialog.io.model.ExportData;
import offset.nodes.client.dialog.io.model.ExportFile;
import offset.nodes.client.view.BaseApplet;

/**
 *
 * @author Walter Lütgenau
 */
public class ExportFileApplet extends BaseApplet {

    JFileChooser dialog = null;
    ServerModel model = null;
    String fromPath = null;
    ResourceBundle bundle = ResourceBundle.getBundle("offset/nodes/client/dialog/view/DialogBundle");

    /** Creates a new instance of NodeChooserApplet */
    public ExportFileApplet() {
    }

    public void startUI() {
        int result = dialog.showDialog(new JFrame(), bundle.getString("export.title"));
        URL path = null;
        try {
            if (result == JFileChooser.APPROVE_OPTION) {
                File selected = dialog.getSelectedFile();

                ExportFile.Request exportFileRequest = new ExportFile.Request();
                exportFileRequest.setPath(fromPath);

                ExportFile.Response exportFileResponse = (ExportFile.Response) model.sendRequest(exportFileRequest);
                if (exportFileResponse.getResult() == ServerResponse.RESULT_SUCCESS) {
                    OutputStream out = new FileOutputStream(selected);
                    ExportData.Request exportDataRequest = new ExportData.Request();

                    boolean goOn = true;
                    do {
                        exportDataRequest.setDataId(exportFileResponse.getExportId());

                        ExportData.Response exportDataResponse = (ExportData.Response) model.sendRequest(exportDataRequest);
                        if (exportDataResponse.getResult() == ServerResponse.RESULT_SUCCESS)
                            out.write(exportDataResponse.getData());
                        if (exportDataResponse.isLastFragment())
                            goOn = false;
                    } while (goOn);

                    out.close();
                }

            }

            path = new URL(getParameter("repository") + "/" + fromPath);

        } catch (Exception e) {
            e.printStackTrace();
        }

        getAppletContext().showDocument(path);
    }

    @Override
    public void initUI() {
        try {
            fromPath = getParameter("path");
            String url = getParameter("service");

            model = new ServerModel(new URL(url));
            dialog = new JFileChooser();
            dialog.setDialogType(JFileChooser.SAVE_DIALOG);
            dialog.setSelectedFile(new File(new File(fromPath).getName()));
            dialog.setLocation(100, 100);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
